package com.jd.fruit.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;

public class JsonConvertUtil {
	private static  List<Record> utilList=null;
	private static String jsonStr ="";
	/**
	 * 将栏目list数据转换成json
	 * 
	 * @param list
	 * @return
	 */
	public static JSONArray tranformToJSON(List<Record> recordList,String rolemap) {
		JSONArray jsonArray = new JSONArray();
		String json = "";
		String id = "";
		for (Record record : recordList) {
			try {
				json = record.toJson();
				json = json.replaceAll("\"parent_id\"", "\"pId\"");
				json = json.replaceAll("\"cat_name\"", "\"name\"");
				if (json.contains("\"pId\":null")) {
					json = json.replaceAll("\"pId\":null", "\"pId\":0");
				}
				id = json.substring(json.indexOf("\"id\":") + 6, json.indexOf(",") - 1);
				if (rolemap != null) {
					if (rolemap.contains(id)) {
						json = json.replaceAll("}", "\\,\"checked\":true}");
					}
				}
				// 忽略大小写
				String wordReg = "[^\"],[^\"]";
				Matcher matcher = Pattern.compile(wordReg).matcher(json);
				StringBuffer sb = new StringBuffer();
				while (matcher.find()) {
					String group = matcher.group().replaceAll(",", ".");
					matcher.appendReplacement(sb, group);// 原文的大小写没有发生变化
				}
				matcher.appendTail(sb);
				JSONObject object = JSONObject.parseObject(sb.toString());
				jsonArray.add(object);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}

		return jsonArray;
	}

	/**
	 * 转换treeGrid格式
	 * 
	 * @param recordList
	 * @return
	 */
	public static JSONArray tranformToJSON(List<Record> recordList) {
		JSONArray jsonArray = new JSONArray();
		String json = "";
		String id = "";
		for (Record record : recordList) {
			try {
				json = record.toJson();
				json = json.replaceAll("\"parent_id\"", "\"pId\"");
				json=json.replaceAll("\"is_display\":0", "\"isdisplay\":\"否\"");
				json=json.replaceAll("\"is_display\":1", "\"isdisplay\":\"是\"");
				json=json.replaceAll("\"nav_is_display\":0", "\"navisdisplay\":\"否\"");
				json=json.replaceAll("\"nav_is_display\":1", "\"navisdisplay\":\"是\"");
				if (json.contains("\"pId\":null")) {
					json = json.replaceAll("\"pId\":null", "\"pId\":0");
					json = json.replaceAll("}", "\\,\"isParent\":true}");
				}
				/*id = json.substring(json.indexOf("\"id\":") + 6, json
						.indexOf(",") - 1);*/
				// 忽略大小写
				String wordReg = "[^\"],[^\"]";
				Matcher matcher = Pattern.compile(wordReg).matcher(json);
				StringBuffer sb = new StringBuffer();
				while (matcher.find()) {
					String group = matcher.group().replaceAll(",", ".");
					matcher.appendReplacement(sb, group);// 原文的大小写没有发生变化
				}
				matcher.appendTail(sb);
				JSONObject object = JSONObject.parseObject(sb.toString());
				jsonArray.add(object);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
		return jsonArray;
	}
	
	/**
	 * 转换成treegrid格式
	 * @param jsonArray
	 * @return
	 */
	public static String getFormatData(List<Record> recordList) {
		String json="";
		jsonStr="";
		for (Record record : recordList) {
			if(StrKit.isBlank(record.getStr("parent_id"))){
				formatJason(recordList,record);
			}
		}
          //替换每次循环最后一个逗号。
		  jsonStr=jsonStr.replace("},]", "}]");
		  json="["+jsonStr.substring(0,jsonStr.lastIndexOf(","))+"]";
		return json;
	}
	
	public static  void formatJason(List<Record> recordList,Record current){
		String json="";
		json=current.toJson();
		json = json.replaceAll("\"parent_id\"", "\"pId\"");
		json=json.replaceAll("\"is_display\":0", "\"isdisplay\":\"否\"");
		json=json.replaceAll("\"is_display\":1", "\"isdisplay\":\"是\"");
		json=json.replaceAll("\"nav_is_display\":0", "\"navisdisplay\":\"否\"");
		json=json.replaceAll("\"nav_is_display\":1", "\"navisdisplay\":\"是\"");
		if (json.contains("\"pId\":null")) {
			json = json.replaceAll("\"pId\":null", "\"pId\":0");
			json = json.replaceAll("}", "\\,\"isParent\":true}");
		}
		jsonStr+= json.replaceAll("}", "\\,\"children\":[");
		if(hasChild(recordList,current)){
			utilList=getChildList(recordList,current);
			for (Record record : utilList) { 
				formatJason(recordList,record);
			}
		}
		jsonStr +="]},";
	}
	 
   public static boolean hasChild(List<Record> list, Record current) {   
	  return getChildList(list,current).size() > 0 ? true : false;  
   }
   
   public static List<Record> getChildList(List<Record> list, Record current) { // 得到子节点列表  
	   List<Record> li = new ArrayList<Record>();  
	   Record tempData=null;
	   String pId=current.getStr("id");
	   String tempStr=null;
       for (int i = 0; i < list.size(); i++) {
    	   tempData=list.get(i);
    	   tempStr=tempData.getStr("parent_id");
    	   if(StrKit.notBlank(tempStr)&&tempStr.equals(pId)){
    		   li.add(tempData);
    	   }
	   }
	  return li;  
   }  

	
	
}
